package edu.jptest.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.jptest.bean.Examine;
import edu.jptest.util.GetDataBaseConnection;

public class ExamineDAO {
	// 插入考务
	public boolean insertExamine(Examine examine) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into examine(user_id,e_p_id,score,question_typescore,examine_time) values(?,?,?,?,?)";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examine.getUserId());
			pstm.setInt(2, examine.getExaminePaperId());
			pstm.setFloat(3, examine.getScore());
			pstm.setFloat(4, examine.getQuestionTypescore());
			pstm.setDate(5, examine.getExamineTime());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 删除考务
	public boolean deleteExamine(int examine_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "delete from examine where examine_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examine_id);
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 更该考务
	public boolean updateExamine(Examine examine) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "update examine set user_id=?,e_p_id=?,score=?,question_typescore=?,examine_time=? where examine_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examine.getUserId());
			pstm.setInt(2, examine.getExaminePaperId());
			pstm.setFloat(3, examine.getScore());
			pstm.setFloat(4, examine.getQuestionTypescore());
			pstm.setDate(5, examine.getExamineTime());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 通过考务id，查询考务对象
	public Examine getExamineById(int examine_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		Examine examine = new Examine();
		PreparedStatement pstm = null;
		try {
			String sql = "select * from examine where examine_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examine_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examine.setExamineId(resultSet.getInt(1));
				examine.setUserId(resultSet.getInt(2));
				examine.setExaminePaperId(resultSet.getInt(3));
				examine.setScore(resultSet.getFloat(4));
				examine.setQuestionTypescore(resultSet.getFloat(5));
				examine.setExamineTime(resultSet.getDate(6));
				return examine;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return new Examine();
	}

	// 通过用户Id，查询该用户的所有考务id
	public List<Integer> getAllExaminesByUserId(int user_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examinePapresId = new ArrayList<Integer>();
		try {
			String sql = "select examine_id from examine where user_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, user_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examinePapresId.add(resultSet.getInt(1));
			}
			return examinePapresId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过试卷id，查询该试卷下的所有考务id
	public List<Integer> getAllExaminesByExaminePaperId(int examinepaper_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examinePapresId = new ArrayList<Integer>();
		try {
			String sql = "select examine_id from examine where e_p_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examinepaper_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examinePapresId.add(resultSet.getInt(1));
			}
			return examinePapresId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 根据试卷单元、用户id，获得相关考务id（内包含试卷及其它具体信息）
	public List<Integer> getExamineIdByUnit(int user_id, int unit) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examinePapresId = new ArrayList<Integer>();
		try {
			String sql = "select examine_id from examine where user_id=? and e_p_id in (select e_p_id from examine_paper where unit_begin=? and unit_end=?)";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, user_id);
			pstm.setInt(2, unit);
			pstm.setInt(3, unit);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examinePapresId.add(resultSet.getInt(1));
			}
			return examinePapresId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 根据试卷课别、用户id，获得相关考务id（内包含试卷及其它具体信息）
	public List<Integer> getExamineIdByLesson(int user_id, int lesson) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examinePapresId = new ArrayList<Integer>();
		try {
			String sql = "select examine_id from examine where user_id=? and e_p_id in (select e_p_id from examine_paper where lesson_begin=? and lesson_end=?)";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, user_id);
			pstm.setInt(2, lesson);
			pstm.setInt(3, lesson);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examinePapresId.add(resultSet.getInt(1));
			}
			return examinePapresId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 根据试卷类型、用户id，获得相关考务id
	public List<Integer> getExamineIdByExaminePaperType(
			String examinepaper_type, int user_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examinePapresId = new ArrayList<Integer>();
		try {
			String sql = "select examine_id from examine where user_id=? and e_p_id in (select e_p_id from examine_paper where paper_type=?)";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, user_id);
			pstm.setString(2, examinepaper_type);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examinePapresId.add(resultSet.getInt(1));
			}
			return examinePapresId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过用户id、试卷id，查询某个用户某次考试的考务信息
	public int getExamineByUserIdAndExaminePaperId(int user_id,
			int examinepapaer_id, java.sql.Date date) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select examine_id from examine where examine_id in (select examine_id from exmaine where user_id=? and e_p_id=?) and examine_time=?";// 三个条件
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, user_id);
			pstm.setInt(2, examinepapaer_id);
			pstm.setDate(3, date);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				return resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;
	}

	// // 通过试卷分数、试卷id、考试时间，获得所有该分数的考务id（内包含用户及其它具体信息）
	// public List<Integer> getExaminesIdByScore(int examine_id, Date date,
	// float score) {
	//
	// return null;
	// }

	// 通过试卷分数、用户id，获得所有小于该分数的考务id
	public List<Integer> getExamineIdByScore(int user_id, float score) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		ArrayList<Integer> examinesId = new ArrayList<Integer>();
		try {
			String sql = "select examine_id from examine where user_id=? and score<?";// 三个条件
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, user_id);
			pstm.setFloat(2, score);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examinesId.add(resultSet.getInt(1));
			}
			return examinesId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过试卷分数、用户id，获得所有该分数段的考务id
	public List<Integer> getExamineIdByUserAndScoreScope(int user_id,
			float minScore, float maxScore) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		ArrayList<Integer> examinesId = new ArrayList<Integer>();
		try {
			String sql = "select examine_id from examine where score in (select score from examine where score>=? and score<=?) and user_id = ?";// 三个条件
			pstm = connection.prepareStatement(sql);
			pstm.setFloat(1, minScore);
			pstm.setFloat(2, maxScore);
			pstm.setInt(3, user_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examinesId.add(resultSet.getInt(1));
			}
			return examinesId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过试卷分数段、试卷id、考试时间，获得该分数段的考务id（内包含用户及其他具体信息）
	public List<Integer> getExamineIdByScoreScope(int examinePaper_id,
			java.sql.Date date, float min_score, float max_score) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		ArrayList<Integer> examinesId = new ArrayList<Integer>();
		try {
			String sql = "select examine_id from examine where examine_time =? and examine_id in (select examine_id from examine where e_p_id=? and examine_id in (select examine_id from examine where score>=? and score<=?))";// 三个条件
			pstm = connection.prepareStatement(sql);
			pstm.setInt(2, examinePaper_id);
			pstm.setFloat(3, min_score);
			pstm.setFloat(4, max_score);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examinesId.add(resultSet.getInt(1));
			}
			return examinesId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过考试时间段、用户id，获得某时间段的考务
	public List<Integer> getExaminesIdByTimeScope(int user_id,
			java.sql.Date startDate, java.sql.Date endDate) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		ArrayList<Integer> examinesId = new ArrayList<Integer>();
		try {
			String sql = "select examine_id from examine where examine_time in (select examine_time from examine where examine_time>=? and examine_time<=?) and user_id = ?";// 三个条件
			pstm = connection.prepareStatement(sql);
			pstm.setDate(1, startDate);
			pstm.setDate(2, endDate);
			pstm.setInt(3, user_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examinesId.add(resultSet.getInt(1));
			}
			return examinesId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}
}
